package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class MoveFriendToOtherCatalogContract {
    public MoveFriendToOtherCatalogContract() { }
    public MoveFriendToOtherCatalogContract(String friendID, String oldCatalog, String newCatalog)
    {
        this.FriendID = friendID;
        this.OldCatalog = oldCatalog;
        this.NewCatalog = newCatalog;
    }

    public String FriendID;
    public String OldCatalog ;
    public String NewCatalog ;

    public byte[] serialize() throws Exception {
        byte[] bFriendID = FriendID.getBytes(StandardCharsets.UTF_8);
        byte[] bNewCatalog = NewCatalog.getBytes(StandardCharsets.UTF_8);
        byte[] bOldCatalog = OldCatalog.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bFriendID.length + 4 + bNewCatalog.length + 4 + bOldCatalog.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bFriendID.length);
        body.writeBytes(bFriendID);
        body.writeInt(bNewCatalog.length);
        body.writeBytes(bNewCatalog);
        body.writeInt(bOldCatalog.length);
        body.writeBytes(bOldCatalog);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.FriendID= SerializeUtils.readStrIntLen(buffer);
        this.NewCatalog=SerializeUtils.readStrIntLen(buffer);
        this.OldCatalog=SerializeUtils.readStrIntLen(buffer);
    }
}
